package broker

import (
	"go-micro.dev/v4/broker"
	"ota/bll"
	log "ota/collector/logger"
	"ota/config"
)

var PubSub broker.Broker
var Consumer *consumer

func Init() func() {
	switch config.Conf.Broker.Type {
	case "rabbitmq":
		PubSub = InitRabbit()
	//case "ali_rabbitmq":
	//	PubSub = InitAliRabbit()
	case "nats":
		PubSub = InitNats()
	}

	if PubSub != nil {
		Consumer = NewConsumer(PubSub)
		Consumer.Init()
		bll.SetPublisher(PubSub)
	}

	return func() {
		if PubSub != nil {
			if err := PubSub.Disconnect(); err != nil {
				log.Error(err)
			}
		}
	}
}
